'use strict';

angular.module('app')
    .controller('importController', [ '$rootScope', '$scope', '$http', '$state',
        function($rootScope, $scope, $http, $state) {
            $scope.title = '导入Excel';
            $scope.param = { };
            $scope.loading = false;
            $scope.value2 = "";

            $scope.showInfo=function(obj){
                var a=document.getElementById("file").files[0];
                // var filename = "文件名：" + obj.value.substring(pos+1);
                document.getElementById("fileName").innerText ="导入文件："+ a.name;
                document.getElementById("msg").innerText="";
            }

            //导入EXCEL
            $scope.uploadFile = function (){
                 var fileObj = document.getElementById("file").files[0]; // 获取文件对象
                var type=document.getElementById("type").value;
                var url;
                // console.log(type);
                if(null==type||type==''){
                    $scope.alertModal({'content':'类型不能为空'});
                    return;
                }else if(type=='1'){
                    url=$scope.app.project_name+'/tools/import';
                }else if(type=='2'){
                    url=$scope.app.project_name+'/tools/importIP';
                }
                if(fileObj){
                    //console.log(fileObj);
                    // FormData 对象
                    var form = new FormData();
                    form.append("file", fileObj);// 文件对象
                    $.ajax({
                        url :url,
                        type: 'POST',
                        data: form,
                        async: false,
                        cache: false,
                        contentType: false,
                        processData: false
                    }).then(function(result) {
                        console.log(result);
                        $scope.loading = false;
                        if (result.msge!='') {
                            //  $scope.alertModal({"content":"操作失败!"})
                            document.getElementById("msg").innerText="导入失败！"+result.msge;
                            $scope.param = result ;
                            $scope.param.type = type;

                        } else {
                            //   $scope.alertModal({"content":"操作成功!"});
                            document.getElementById("msg").innerText="导入成功！"+result.msge;
                            $scope.param = result ;
                            $scope.param.type = type;
                        }
//
                    });
                }else{
                    $scope.alertModal({"content":"未选择文件"});
                }
                $scope.isDisabled = false;
            }

            //导出模板
            $scope.fileDown = function () {
                var type=document.getElementById("type").value;
                var name;
                if(null==type||type==''){
                    $scope.alertModal({'content':'类型不能为空'});
                    return;
                }else if(type=='1'){
                    name='personTag.xls';
                }else if(type=='2'){
                    name='IPTemplet.csv';
                }
                window.location.href = $scope.app.project_name + "/templet/download?fileName=" + name;
            }
        } ]);